from lxml import etree
import requests
import os
from fake_useragent import UserAgent
import json
import random
class MyBase:
    api_url = "https://dps.kdlapi.com/api/getdps/?orderid=969446626073773&num=20&pt=1&format=json&sep=1"
    headers3 = {
        'User-Agent': UserAgent().random,
    }


    def __init__(self,*args,**kwargs):
        pass
    def get_proxy(self):
        proxy_ip_s = requests.get(self.api_url).text
        proxy_list = json.loads(proxy_ip_s)['data']['proxy_list']
        ip_port = random.choice(proxy_list)
        # 验证ip的可用性
        proxies = {"http": "http://" + ip_port, "https": "https://" + ip_port}

    def my_xpath(self,data,ruler): #xpath打包方法
        return etree.HTML(data).xpath(ruler)

    def get_interger(self, data): #转换数字
        try:
            res = int(data)
        except:
            try:
                res = float(data)
            except:
                res = data
        return res

    def is_empty(self, data):  # 处理数据为空的字段 str转int float
        if isinstance(data, list):
            if data != []:
                jieguo = data[0].strip()
                jieguo = self.get_interger(jieguo)
            else:
                jieguo = ''
        else:
            jieguo = self.get_interger(data)
        return jieguo

    def request_url(self,url,ma=0): #请求方法
        shu = ma
        shu += 1
        try:
            resp = requests.get(url=url, headers=self.headers3)# proxies=self.proxy, verify=False,timeout=5, allow_redirects=False
            if resp.status_code == 200 and resp:
                self.a = 0
                # print('hhhhh')
                return resp
            else:
                raise Exception
        except Exception:
            if shu >=10:
                raise Exception('请求失败！')
            else:
                resp = self.request_url(url,shu)
                return resp

    def parse_content(self): #提取游戏列表
        pass

    def get_detail_content(self): #请求失败循环请求
        m = 0
        while m<=9:
            game_url_list = self.parse_content()
            if game_url_list:
                return game_url_list
            else:
                m+=1
                print('list为空')
        # time.sleep(1)
        raise Exception('请求10次失败！')

    def take_list(self,list_url): #处理列表内容
        lis = []
        for i in list_url:
            ts = i.split('/')[-1]
            lis.append(ts)
        return  lis

    def mk_file(self,file):
        try:
            os.mkdir(file)
        except FileExistsError:
            pass

    def save_file(self,dir,content,moshi):
        with open(dir,moshi) as f:
            f.write(content)

    def run_cmd(self,*args): #执行cmd命令
        save_dir,mingling = args
        os.chdir(save_dir) #切换目录
        os.system(mingling)

    def del_file(self,*args): #删除ts文件
        current_dir, = args
        files = os.listdir(current_dir)
        for video_ts_name in files:
            del_dir = os.path.join(current_dir, video_ts_name)
            # print(del_dir)
            if os.path.exists(del_dir):
                os.remove(del_dir)

    def run(self):
        pass

if __name__=='__main__':
    pass